
package ucm.module;

import air_cp5620.AirInfo;
import air_cp5620.dao.DAO;
import javax.swing.JOptionPane;
import java.sql.*;
import java.util.Vector;
import ucm.view.UC_SelInfoResAllV;
import ucm.view.UC_SelInfoResAllV2;
import ucm.view.UC_SelInfoResOneV;


public class UC_SelInfoM {

    private DAO dbCon = null;

    public UC_SelInfoM() {
        dbCon = new DAO();
    }


    public void selectAllByCon(String start_time,String end_city) {
        String sql = null;
        String condition = "";

        Connection con = dbCon.getConnection();
        if (con == null) {
            JOptionPane.showMessageDialog(null, "Don't conn！");
        }
        try {

            if (!start_time.equals("")) {
                condition += "start_time=date_format('" + start_time + "','%Y-%m-%d %H:%i:%s')";
            }
            if (!end_city.equals("")) {
                if (condition.equals("")) {
                    condition += "end_city='" + end_city + "'";
                }
                else {
                    condition += " and end_city='" + end_city + "'";
                }
            }

            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "select * from tbl_air_info where " + condition ;
            ResultSet res = stmt.executeQuery(sql);
            
            Vector<AirInfo> airInfo_vec = new Vector<AirInfo>();
            while(res.next())
            {
                AirInfo airinfo = new AirInfo(res.getString("id"),
                        res.getString("start_city"),
                        res.getString("end_city"),
                        res.getString("start_time"),
                        res.getString("end_time"),
                        res.getInt("ticketcount"),
                        res.getInt("price"));
                airInfo_vec.add(airinfo);
            }

            UC_SelInfoResAllV uc_sirv = new UC_SelInfoResAllV(airInfo_vec);
            uc_sirv.setVisible(true);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Create air info false！");
            e.printStackTrace();
        }
    }


    public void selectById(String id) {
        String sql = null;

        Connection con = dbCon.getConnection();
        if (con == null) {
            JOptionPane.showMessageDialog(null, "Don't conn！");
        }
        try {
            if (id.equals("")) {
                JOptionPane.showMessageDialog(null, "error,id is not null！");
            }

            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "select * from tbl_air_info where id='" + id + "'" ;
            ResultSet res = stmt.executeQuery(sql);

            Vector<AirInfo> airInfo_vec = new Vector<AirInfo>();
            while(res.next())
            {
                AirInfo airinfo = new AirInfo(res.getString("id"),
                        res.getString("start_city"),
                        res.getString("end_city"),
                        res.getString("start_time"),
                        res.getString("end_time"),
                        res.getInt("ticketcount"),
                        res.getInt("price"));
                airInfo_vec.add(airinfo);
            }

            UC_SelInfoResOneV uc_sirov = new UC_SelInfoResOneV(airInfo_vec);
            uc_sirov.setVisible(true);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Create air info false！");
            e.printStackTrace();
        }
    }


        public void selectByAll(String userid) {
        String sql = null;

        Connection con = dbCon.getConnection();
        if (con == null) {
            JOptionPane.showMessageDialog(null, "Don't conn！");
        }
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.CONCUR_UPDATABLE);
            sql = "select * from tbl_air_info where isdelete='0'" ;
            ResultSet res = stmt.executeQuery(sql);

            Vector<AirInfo> airInfo_vec = new Vector<AirInfo>();
            while(res.next())
            {
                AirInfo airinfo = new AirInfo(res.getString("id"),
                        res.getString("start_city"),
                        res.getString("end_city"),
                        res.getString("start_time"),
                        res.getString("end_time"),
                        res.getInt("ticketcount"),
                        res.getInt("price"));
                airInfo_vec.add(airinfo);
            }

            UC_SelInfoResAllV2 uc_sirov2 = new UC_SelInfoResAllV2(airInfo_vec,userid);
            uc_sirov2.setVisible(true);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Create air info false！");
            e.printStackTrace();
        }
    }
}
